package com.message.api.log.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.base.annotation.SysLog;
import com.base.helper.BaseController;
import com.base.helper.Result;
import com.message.api.log.entity.AcLog;
import com.message.api.log.service.AcLogService;
import com.message.api.log.vo.AcLogVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author mzl
 * @since 2020-07-09
 */
@Api(tags = "日志")
@RestController
@RequestMapping("/acLog")
public class AcLogController extends BaseController {

    private static final Logger       LOG = LoggerFactory.getLogger(AcLogController.class);
    @Autowired
    private              AcLogService entityService;

    @SysLog(action = "acLog:findByPage", value = "系统日志：接口访问日志列表")
    @ApiOperation(value = "分页查询日志", notes = "分页查询日志")
    @RequestMapping(method = RequestMethod.GET)
    @ApiImplicitParams({@ApiImplicitParam(name = "size", value = "分页大小", paramType = "query"),
            @ApiImplicitParam(name = "current", value = "当前页面：从1开始", paramType = "query")})
    public Result<IPage<AcLogVo>> findByPage(final AcLogVo example, final Page page) {
        IPage<AcLogVo> records = entityService.findByPage(example, page);
        return new Result(true, records);
    }

    @SysLog(action = "acLog:delete", value = "系统日志：删除接口访问日志")
    @ApiOperation(value = "删除日志")
    @RequestMapping(value = "{id}", method = {RequestMethod.DELETE})
    @ApiImplicitParam(name = "id", value = "日志ID", required = true, paramType = "path")
    public Result delete(@PathVariable final Integer id) {
        entityService.removeById(id);
        return new Result(true, "成功删除日志", null);
    }

    @SysLog(action = "acLog:one", value = "系统日志：查询接口访问日志详情")
    @ApiOperation(value = "查询单个日志")
    @RequestMapping(value = "{id}", method = {RequestMethod.GET})
    @ApiImplicitParam(name = "id", value = "日志ID", required = true, paramType = "path")
    public Result one(@PathVariable final Integer id) {
        AcLog entity = entityService.getById(id);
        return new Result(true, entity);
    }

    @SysLog(action = "acLog:add", value = "系统日志：添加接口访问日志")
    @ApiOperation(value = "添加日志")
    @RequestMapping(method = {RequestMethod.POST})
    public Result add(@Valid @RequestBody final AcLog entity) {
        entityService.save(entity);
        return new Result(true, "成功保存日志", null);
    }

    @SysLog(action = "acLog:update", value = "系统日志：修改接口访问日志")
    @ApiOperation(value = "修改日志")
    @RequestMapping(method = {RequestMethod.PUT})
    public Result update(@Valid @RequestBody final AcLog entity) {
        if (null == entity.getId()) {
            throw new RuntimeException("id不能为空");
        }
        entityService.updateById(entity);
        return new Result(true, "成功修改日志", null);
    }

    @SysLog(action = "acLog:all", value = "系统日志：全部接口访问日志")
    @ApiOperation(value = "全部日志")
    @RequestMapping(value = "all", method = RequestMethod.GET)
    public Result<List<AcLogVo>> all(AcLogVo example) {
        List<AcLogVo> entitys = entityService.findByExample(example);
        if (null != entitys) {
            return new Result<>(true, entitys);
        }
        return new Result<>(true, Collections.emptyList());
    }

}

